﻿//43.字符串相乘
class Solution {
public:
    //字符串相加
    string addStrings(string num1, string num2) {
        int m = num1.size() - 1, n = num2.size() - 1;
        string ans;
        ans.reserve(max(m, n) + 2);

        int count = 0;
        while (m >= 0 || n >= 0)
        {
            int x = m >= 0 ? num1[m] - '0' : 0;
            int y = n >= 0 ? num2[n] - '0' : 0;
            int sum = x + y + count;
            count = sum / 10;
            ans += sum % 10 + '0';
            m--;
            n--;
        }

        if (count != 0)
        {
            ans += count + '0';
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
    string multiply(string num1, string num2) {
        if (num1 == "0" || num2 == "0")
        {
            return "0";
        }

        int m = num1.size() - 1;

        string ans("0");
        while (m >= 0)
        {
            string n1;
            int n = num2.size() - 1;
            int count = 0;

            int x = num1[m] - '0';
            while (n >= 0)
            {

                int y = num2[n--] - '0';
                int sum = (x * y) + count;
                count = sum / 10;
                n1 += sum % 10 + '0';
            }
            if (count != 0)
            {
                n1 += count + '0';
            }
            n1.insert(0, num1.size() - 1 - m, '0');
            reverse(n1.begin(), n1.end());

            ans = addStrings(ans, n1);

            m--;
        }

        return ans;
    }
};